(function(w, $) {
    'use strict';

    var DEFAULT = {
        data : undefined,
        rootId : 0,
        id : "id",
        parentId : "parentId",
        children : "children",
        sort : "",
    };

    var TREE_METHOD = {
        toTree : function (list,pid) {
            var result = [];//一个存放结果的临时数组
            if(!list || list.length < 1){ return result ;};
            for (var i in list) {
                if (list[i][this.options.parentId] == pid) {//如果当前项的父id等于要查找的父id，进行递归
                    list[i][this.options.children] = this.toTree(list, list[i][this.options.id]);
                    if(list[i][this.options.children].length < 1){
                        list[i][this.options.children] = null;
                    }else if(this.options.sort){//排序
                        var srot = this.options.sort;
                        list[i][this.options.children].sort(function(a, b){
                            return a.hasOwnProperty(srot) && b.hasOwnProperty(srot) ? a[srot] - b[srot] : 0;
                        })
                    }
                    result.push(list[i]);//把当前项保存到临时数组中
                }
            }
            return result;//递归结束后返回结果
        },
        addRelevance : function(tree,pid){
            if(Array.isArray(tree)){
                for(var key in tree){
                    tree[key][this.options.parentId] = pid ? pid : 0;
                    tree[key][this.options.id]  = this.options.temp_id = this.options.temp_id ? ++this.options.temp_id : tree[key][this.options.parentId]+1;
                    if(tree[key][this.options.children]){
                        this.addRelevance(tree[key][this.options.children],tree[key][this.options.id]);
                    }
                }
            }else {
                tree[this.options.parentId] = pid ? pid : 0;
                tree[this.options.id]  = this.options.temp_id = this.options.temp_id ? ++this.options.temp_id : tree[this.options.parentId]+1;
                if(tree[this.options.children]){
                    this.addRelevance(tree[this.options.children],tree[this.options.id]);
                }
            }
            return tree;
        },
        toArray : function (tree,key) {
            var array = [];
            if(Array.isArray(tree)){
                for (var i in tree) {
                    var item  = tree[i] ;
                    var children = item[this.options.children];
                    delete  item[this.options.children];
                    array.push(item);
                    if (children) {
                        array = array.concat(this.toArray(children))
                    }
                }
            }else {
                var item  = tree ;
                var children = item[this.options.children];
                delete  item[this.options.children];
                array.push(item);
                if (children) {
                    array = array.concat(this.toArray(children))
                }
            }

            return array;
        }
    };


   var TREE = function (options){
        this.options = $.extend({},DEFAULT,typeof options == 'object' && options ? options : {});
        if(!this.options || !this.options.data || this.options.data.length < 1){ return [] ;};
        $.extend(this,TREE_METHOD);
    };

    w.Tree = {
        toTree : function (options) {
            var tree = new TREE(options);
            return tree.toTree(tree.options.data,tree.options.rootId);
        },
        toArray : function (options) {
            var tree = new TREE(options);
            return tree.toArray(tree.options.data,tree.options.rootId);
        },
        addRelevance : function (options) {
            var tree = new TREE(options);
            return tree.addRelevance(tree.options.data,tree.options.rootId);
        }
    };

}(window, jQuery));